package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;

import DAO.DAOReserva;

public class AdministracionPersistenciaReserva {
	
	private static AdministracionPersistenciaReserva instance;
	
	private AdministracionPersistenciaReserva(){
		
	}
	 
	public static AdministracionPersistenciaReserva getInstance(){
		if(instance==null)
			instance = new AdministracionPersistenciaReserva();
		return instance;
	}

	public void insert(DAOReserva reserva){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("insert into Reservas values (?,?,?,?,?,?,?)");
			ps.setString(1, reserva.getPatente());
			ps.setInt(2, reserva.getDniCliente());
			ps.setString(3, reserva.getNombreCliente());
			ps.setString (4, reserva.getApellidoCliente());
			ps.setString(5, reserva.getDesde());
			ps.setString (6,reserva.getHasta());
			ps.setString(7, reserva.getMarca());
			ps.setString(8, reserva.getModelo());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al insertar Reserva "+e.getMessage());
		}
	}

	public void update(DAOReserva reserva){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("" +
					"update Reservas set patente = ?, dniCliente = ?, nombreCliente = ?, apellidoCliente =? ,desde = ?, hasta = ? , marca = ?, modelo =?" +
					"where nroReserva = ?");
			ps.setString(1, reserva.getPatente());
			ps.setInt(2, reserva.getDniCliente());
			ps.setString(3, reserva.getNombreCliente());
			ps.setString (4, reserva.getApellidoCliente());
			ps.setString(5, reserva.getDesde());
			ps.setString (6,reserva.getHasta());
			ps.setString(7, reserva.getMarca());
			ps.setString(8, reserva.getModelo());
			ps.setInt(9, reserva.getNroReserva());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al modificar Reserva"+e.getMessage());
		}
	}

	public void delete(DAOReserva p){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
						
			PreparedStatement ps2 = con.prepareStatement("delete from Reservas where nroReserva = ?");
			ps2.setInt(1, p.getNroReserva());
			ps2.execute();
			
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al borrar Reserva "+e.getMessage());
		}
	}

}
